/**
 * 组件安装
 * npm install gulp-util gulp-imagemin gulp-ruby-sass gulp-minify-css gulp-jshint gulp-uglify gulp-rename gulp-concat gulp-clean gulp-livereload tiny-lr --save-dev
 */

// 引入 gulp及组件
var gulp    = require('gulp'),                 //基础库
    imagemin = require('gulp-imagemin'),       //图片压缩
    minifycss = require('gulp-minify-css'),    //css压缩
    jshint = require('gulp-jshint'),           //js检查
    uglify  = require('gulp-uglify'),          //js压缩
    rename = require('gulp-rename'),           //重命名
    concat  = require('gulp-concat'),          //合并文件
    clean = require('gulp-clean');             //清空文件夹

// 样式处理
gulp.task('css-public', function () {

    var cssSrc = [
        './public/css/public/base.css',
        './public/css/public/animate.css',
        './public/css/public/ico.css',
        './public/css/public/tit.css',
        './public/css/public/btn.css',
        './public/css/public/tool.css',
        './public/css/public/ddmenu.css',
        './public/css/public/tip.css',
        './public/css/public/nav.css',
        './public/css/public/vernav.css',
        './public/css/public/table.css',
        './public/css/public/tab.css',
        './public/css/public/panel.css',
        './public/css/public/progress.css',
        './public/css/public/top-notice.css',
        './public/css/public/form.css',
        './public/css/public/list.css',
        './public/css/public/pagebody.css',
        './public/css/public/lrscroll.css',
        './public/css/public/share.css',
        './public/css/public/global.css',
        './public/css/layout.css'
    ],
        cssDst = './public/dist/css';

    gulp.src(cssSrc)
        .pipe(concat('public.css'))
        //.pipe(gulp.dest(cssDst))
        .pipe(rename({ suffix: '.min' }))
        .pipe(minifycss({ "compatibility": "ie7" }))
        .pipe(gulp.dest(cssDst));
});
gulp.task('css-view', function () {

    var cssSrc = [
        '!./public/css/layout.css',
        './public/css/*.css'
    ],
        cssDst = './public/dist/css';

    gulp.src(cssSrc)
        //.pipe(rename({ suffix: '.min' }))
        .pipe(minifycss({ "compatibility": "ie7" }))
        .pipe(gulp.dest(cssDst));
});

// 图片处理
gulp.task('images', function(){
    var imgSrc = [
        './public/images/**/**/**/*'
    ],
        imgDst = './public/dist/images';
    gulp.src(imgSrc)
        .pipe(imagemin())
        .pipe(gulp.dest(imgDst));
});


// js处理
gulp.task('js-public', function () {
    var jsSrc = [
        './public/js/jquery.autocomplete.js',
        './public/js/jquery.lazyload.min.js',
        './public/js/tools.js',
        './public/js/base.global.js',
        './public/js/public.js'
    ],
        jsDst ='./public/dist/js';

    gulp.src(jsSrc)
        .pipe(jshint.reporter('default'))
        .pipe(concat('jquery.plugin.js'))
        //.pipe(gulp.dest(jsDst))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify())
        .pipe(gulp.dest(jsDst));
});
gulp.task('js-view', function () {
    var jsSrc = [
        './public/js/jquery-1.11.3.min.js',
        './public/js/ajaxForm.js',
        './public/js/cart.js',
        './public/js/detail.js',
        './public/js/help.js',
        './public/js/home.js',
        './public/js/jquery.jqzoom.js',
        './public/js/jquery.validation.js',
        './public/js/location.js',
        './public/js/orderList.js',
        './public/js/responsiveslides.min.js',
        './public/js/scroll.js',
        './public/js/userCenter.js',
        './public/js/utils.js'
    ],
        jsDst ='./public/dist/js';

    gulp.src(jsSrc)
        .pipe(jshint.reporter('default'))
        //.pipe(concat('mobile.js'))
        //.pipe(gulp.dest(jsDst))
        //.pipe(rename({ suffix: '.min' }))
        .pipe(uglify())
        .pipe(gulp.dest(jsDst));
});
gulp.task('js-plugin', function(){
    var fileSrc = [
            './public/js/+(fly|layer|linkagesel|localization)/**/*'
        ],
        fileDst = './public/dist/js';
    gulp.src(fileSrc)
        .pipe(gulp.dest(fileDst));
});

//打印订单  生成二维码
gulp.task('js-print', function () {
    var jsSrc = [
            './public/js/jquery.jqprint-0.3.js',
            './public/js/jquery-migrate-1.1.0.js',
            './public/js/jsbarcode/EAN_UPC.js',
            './public/js/jsbarcode/CODE128.js',
            './public/js/jsbarcode/JsBarcode.js'
        ],
        jsDst ='./public/dist/js';

    gulp.src(jsSrc)
        .pipe(jshint.reporter('default'))
        .pipe(concat('jquery.barCodePrint.js'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify())
        .pipe(gulp.dest(jsDst));
});

// 清空所有 运行语句 gulp clean
gulp.task('clean', function() {
    gulp.src(['./public/dist/*'], {read: false})
        .pipe(clean());
});

// 重建css 运行语句 gulp css
gulp.task('css', function(){
    gulp.start('css-public','css-view');
});

// 重建js 运行语句 gulp js
gulp.task('js', function(){
    gulp.start('js-public','js-view','js-plugin','js-print');
});

// 默认任务 清空所有并重建 运行语句 gulp
gulp.task('default', ['clean'], function(){
    gulp.start('css-public','css-view','js-public','js-view','js-plugin','js-print');
});
